// USER START (Optionally insert additional includes)
// USER END
#include "Window.h"
#include "stm32f10x.h"                  // Device header
extern USART cout;
BUTTON_SKINFLEX_PROPS SkinFlexOrig[3];
#pragma push
#pragma diag_suppress 61

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/

// USER START (Optionally insert additional defines)
#define ID_BUTTON_MESUARE (GUI_ID_USER+0)
#define ID_BUTTON_REPLY (GUI_ID_USER+1)
#define ID_BUTTON_SETTING (GUI_ID_USER+2)
#define ID_TEXT_TIME (GUI_ID_USER+3)
// USER END

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

// USER START (Optionally insert additional static data)
WM_HWIN hWin;
// USER END


/*********************************************************************
*
*       Static code
*
**********************************************************************
*/

// USER START (Optionally insert additional static code)

int GetPic(void* p, const U8** ppData, unsigned NumBytes, U32 Off)
{
	static uint8_t buf[1024];
	FIL* pic = (FIL*)p;
	if (NumBytes > sizeof(buf))
		NumBytes = sizeof(buf);
	f_lseek(pic, Off);
	res = f_read(pic, buf, NumBytes, &NumBytes);
	if (res != FR_OK)
		cout << "file read fail" << endl << "res:" << res << endl << __FILE__ << ':' << __LINE__ << endl;
	*ppData = buf;
	return NumBytes;
}
// USER END

/*********************************************************************
*
*       _cbWin
*/
void _cbWin(WM_MESSAGE* pMsg)
{
	static TEXT_Handle ht;
	static BUTTON_Handle hButtonMesuare, hButtonReply, hButtonSetting;
	static BUTTON_SKINFLEX_PROPS Props;
	switch (pMsg->MsgId)
	{
	case WM_CREATE:
	{
		ht = TEXT_CreateEx(100, 104, 120, 32,
			pMsg->hWin, WM_CF_SHOW, TEXT_CF_VCENTER, ID_TEXT_TIME, "");
		TEXT_SetBkColor(ht, GUI_INVALID_COLOR);
		TEXT_SetTextColor(ht, GUI_WHITE);
		TEXT_SetFont(ht, GUI_FONT_D24X32);

		BUTTON_GetSkinFlexProps(&SkinFlexOrig[0], BUTTON_SKINFLEX_PI_PRESSED);
		BUTTON_GetSkinFlexProps(&SkinFlexOrig[1], BUTTON_SKINFLEX_PI_FOCUSSED);
		BUTTON_GetSkinFlexProps(&SkinFlexOrig[2], BUTTON_SKINFLEX_PI_ENABLED);
		{
#define SLE_COLOR ((0xAA << 24) | GUI_BLUE)
			Props.aColorFrame[0] = SLE_COLOR;
			Props.aColorFrame[1] = SLE_COLOR;
			Props.aColorFrame[2] = SLE_COLOR;
			Props.aColorUpper[0] = SLE_COLOR;
			Props.aColorUpper[1] = SLE_COLOR;
			Props.aColorLower[0] = SLE_COLOR;
			Props.aColorLower[1] = SLE_COLOR;
			Props.Radius = 6;
			BUTTON_SetSkinFlexProps(&Props, BUTTON_SKINFLEX_PI_PRESSED);
			Props.aColorFrame[0] = GUI_TRANSPARENT;
			Props.aColorFrame[1] = GUI_TRANSPARENT;
			Props.aColorFrame[2] = GUI_TRANSPARENT;
			Props.aColorUpper[0] = GUI_TRANSPARENT;
			Props.aColorUpper[1] = GUI_TRANSPARENT;
			Props.aColorLower[0] = GUI_TRANSPARENT;
			Props.aColorLower[1] = GUI_TRANSPARENT;
			BUTTON_SetSkinFlexProps(&Props, BUTTON_SKINFLEX_PI_ENABLED);
			BUTTON_SetSkinFlexProps(&Props, BUTTON_SKINFLEX_PI_FOCUSSED);
		}

		hButtonMesuare = BUTTON_CreateEx(46, 195, 45, 45, pMsg->hWin, WM_CF_SHOW | WM_CF_HASTRANS, 0, ID_BUTTON_MESUARE);
		BUTTON_SetBitmap(hButtonMesuare, BUTTON_BI_UNPRESSED, &bmMeasuare);
		BUTTON_SetBitmap(hButtonMesuare, BUTTON_BI_PRESSED, &bmMeasuare);

		hButtonReply = BUTTON_CreateEx(137, 195, 45, 45, pMsg->hWin, WM_CF_SHOW | WM_CF_HASTRANS, 0, ID_BUTTON_REPLY);
		BUTTON_SetBitmapEx(hButtonReply, BUTTON_BI_UNPRESSED, &bmReplyBox, -4, -5);
		BUTTON_SetBitmapEx(hButtonReply, BUTTON_BI_PRESSED, &bmReplyBox, -4, -5);

		hButtonSetting = BUTTON_CreateEx(228, 195, 45, 45, pMsg->hWin, WM_CF_SHOW | WM_CF_HASTRANS, 0, ID_BUTTON_SETTING);
		BUTTON_SetBitmap(hButtonSetting, BUTTON_BI_UNPRESSED, &bmSetting);
		BUTTON_SetBitmap(hButtonSetting, BUTTON_BI_PRESSED, &bmSetting);
	}
	break;
	case WM_PAINT:
	{

		break;
	}
	case WM_NOTIFY_PARENT:
	{
		int NCode = pMsg->Data.v;
		int Id = WM_GetId(pMsg->hWinSrc);
		if (Id == ID_TEXT_TIME)
			break;
		if (NCode == WM_NOTIFICATION_RELEASED) //WM_NOTIFICATION_RELEASED
		{
			WM_DeleteWindow(pMsg->hWin);
			switch (Id)
			{
			case ID_BUTTON_MESUARE:
				CreateMeasureWindow();
				break;
			case ID_BUTTON_REPLY:
				CreateReplyWindow();
				break;
			case ID_BUTTON_SETTING:
				CreateSettingWindow();
				break;
			}
		}
	}
	break;
	case WM_DELETE:
		BUTTON_SetSkinFlexProps(&SkinFlexOrig[0], BUTTON_SKINFLEX_PI_PRESSED);
		BUTTON_SetSkinFlexProps(&SkinFlexOrig[1], BUTTON_SKINFLEX_PI_FOCUSSED);
		BUTTON_SetSkinFlexProps(&SkinFlexOrig[2], BUTTON_SKINFLEX_PI_ENABLED);
		break;
	default:
		WM_DefaultProc(pMsg);
		break;
	}
}
void CreateMainWindow(void)
{
	hWin = WM_CreateWindow(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_CF_SHOW | WM_CF_HASTRANS, _cbWin, 0);//WM_CF_MEMDEV_ON_REDRAW
//WM_CreateTimer(hWin, 0, 0, 0);
}
#pragma pop
